package com.example.httplibrary.download;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;



import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Set;


/**
 * sharedPreference保存数据
 * 
 *
 */
public class DownLoadSPUtils
{
	/*
	 * 存储数据的的key值
	 */
	public DownLoadSPUtils()
	{
		throw new UnsupportedOperationException("cannot be instantiated");
	}

	/**
	 * 将数据保存到SharedPreferences
	 * 
	 * @return
	 */
	public static boolean save(Context mContext,String key, Object obj)
	{

		Editor editor = PreferenceManager.getDefaultSharedPreferences(mContext).edit();

		if (obj instanceof String)
		{
			editor.putString(key, String.valueOf(obj));
		}
		if (obj instanceof Boolean)
		{
			editor.putBoolean(key, Boolean.valueOf(String.valueOf(obj)));
		}
		if (obj instanceof Float)
		{
			editor.putFloat(key, Float.valueOf(String.valueOf(obj)));
		}
		if (obj instanceof Integer)
		{
			editor.putInt(key, Integer.valueOf(String.valueOf(obj)));
		}
		if (obj instanceof Long)
		{
			editor.putLong(key, Long.valueOf(String.valueOf(obj)));
		}
		if (obj instanceof Set)
		{
			editor.putStringSet(key, (Set<String>) (obj));
		}
		return SharedPreferencesCompat.apply(editor);
	}



	public static Object get(Context mContext,String key, Object defaultObject)
	{
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);

		if (defaultObject instanceof String)
		{
			return sp.getString(key, (String) defaultObject);
		} else if (defaultObject instanceof Integer)
		{
			return sp.getInt(key, (Integer) defaultObject);
		} else if (defaultObject instanceof Boolean)
		{
			return sp.getBoolean(key, (Boolean) defaultObject);
		} else if (defaultObject instanceof Float)
		{
			return sp.getFloat(key, (Float) defaultObject);
		} else if (defaultObject instanceof Long)
		{
			return sp.getLong(key, (Long) defaultObject);
		} else if (defaultObject instanceof Set)
		{
			return sp.getStringSet(key, (Set<String>) defaultObject);
		}
		return defaultObject;
	}

	/**
	 * 移除某个key值已经对应的值
	 * 
	 * @param key
	 */
	public static boolean remove(Context mContext,String key)
	{
		Context context =mContext;
		Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
		editor.remove(key);
		return SharedPreferencesCompat.apply(editor);
	}

	/**
	 * 清除所有数据
	 * 
	 */
	public static boolean clear(Context mContext)
	{
		Editor editor = PreferenceManager.getDefaultSharedPreferences(mContext).edit();
		editor.clear();
		return SharedPreferencesCompat.apply(editor);
	}



	/**
	 * 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类
	 * 
	 */
	private static class SharedPreferencesCompat
	{
		private static final Method sApplyMethod = findApplyMethod();

		/**
		 * 反射查找apply的方法
		 * 
		 * @return
		 */
		private static Method findApplyMethod()
		{
			try
			{
				Class clz = Editor.class;
				return clz.getMethod("apply");
			} catch (NoSuchMethodException e)
			{
			}
			return null;
		}

		/**
		 * 如果找到则使用apply执行，否则使用commit
		 * 
		 * @param editor
		 */
		public static boolean apply(Editor editor)
		{
			try
			{
				if (sApplyMethod != null)
				{
					sApplyMethod.invoke(editor);
					return true;
				}
			} catch (IllegalArgumentException e)
			{
			} catch (IllegalAccessException e)
			{
			} catch (InvocationTargetException e)
			{
			}
			return editor.commit();
		}
	}

}